﻿from google.appengine.ext import db
import hashlib

from Classifier import Classifier

class Classification(db.Model):
    name = db.StringProperty()
    iconcolor = db.StringProperty()
    description = db.StringProperty()
    created_on = db.DateTimeProperty(auto_now_add = True)
    
    def __str__(self):
        return 'name: %s\n description: %s' % (self.name, self.description)
    

class Message(db.Model):
    author = db.UserProperty()
    message = db.TextProperty()
    marked_message = db.TextProperty()
    created_on = db.DateTimeProperty(auto_now_add = True)
    last_update = db.DateTimeProperty(auto_now = True)
    classification = db.ReferenceProperty(Classification)
    #class_confidence = db.
    
    def __str__(self):
        #return 'message: %s\n created_on: %s\n author: %s\n classification: %s' % (self.message, self.created_on, self.author.email, self.classification.name)
        return 'message: %s\n classification: %s' % (self.message, self.classification.name)
        #return 'message: %s' % self.message
    
    def md5(self):
        return hashlib.md5(self.message).hexdigest()

    def htmlMsg(self):
        htmlMessage = self.message.replace("&", "&amp;")
        htmlMessage = htmlMessage.replace(">", "&gt;")
        htmlMessage = htmlMessage.replace("<", "&lt;")
        htmlMessage = htmlMessage.replace("\n", "\n<br/>")
        #htmlMessage = htmlMessage.replace(" ", "&nbsp;")
        return htmlMessage
    
    def classify(self):
        return "Accounting"
    
    def markedHtmlMsg(self):
        classifier = Classifier()
        goods_name, unit, weight, money, markedHTML = classifier.getMarkedHTML(self.htmlMsg())
        #print markedHTML
        return markedHTML

class User(db.Model):
    user = db.UserProperty()
    iconcolor = db.StringProperty()
    description = db.StringProperty()
    access_count = db.IntegerProperty()
    operation_count = db.IntegerProperty()
    created_on = db.DateTimeProperty(auto_now_add = True)
    last_update = db.DateTimeProperty(auto_now = True)
    
    def __str__(self):
        return 'user: %s\n description: %s' % (self.nickname, self.description)

class AccountItem(db.Model):
    author = db.UserProperty()
    name = db.StringProperty()
    quantity = db.StringProperty()
    weight = db.StringProperty()
    money = db.StringProperty()
    created_on = db.DateTimeProperty(auto_now_add = True)
    last_update = db.DateTimeProperty(auto_now = True)
    message = db.ReferenceProperty(Message)